package com.aivin.lib_readnotes.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.aivin.lib_base.tool.RouteUtils;
import com.aivin.lib_readnotes.R;
import com.aivin.lib_readnotes.SelectCallBack;
import com.aivin.lib_readnotes.adapter.BookShelfAdapter;
import com.aivin.lib_widget.base.BaseActivity;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.gyf.immersionbar.ImmersionBar;
import com.wk.bmoblib.bean.BookPureText;
import com.wk.bmoblib.tool.DataTool;

import java.util.List;

import butterknife.BindView;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import com.aivin.lib_widget.bean.MarginDecoration;


/**
 * 书架
 */
@Route(path = RouteUtils.BookshelfActivity)
public class BookshelfActivity extends BaseActivity {
    private RecyclerView bookShelfRv ;

    TextView errorInfoTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bookshelf);
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarColor( R.color.color_2e4a7e)
                .init();

       initView();

       showLoadDialog2(this);
       DataTool.getInstance().getPureTextBooks(findListenerBook );
    }



    private void initView(){
        errorInfoTv =  findViewById(R.id.errorInfoTv);
        bookShelfRv =  findViewById(R.id.bookShelfRv);
        bookShelfRv.setHasFixedSize(true);
        bookShelfRv.setLayoutManager(new GridLayoutManager(this, 4));
        bookShelfRv.addItemDecoration(new MarginDecoration(this));
    }


    private FindListener<BookPureText>  findListenerBook =new FindListener<BookPureText>() {
        @Override
        public void done(List<BookPureText> data, BmobException e) {
            if(data != null && data.size()>0 )   {
                updateChapters(data) ;
            }

            if(e!=null){
                errorInfoTv.setVisibility(View.VISIBLE);
                errorInfoTv.setText(""+e.getMessage());
            }else{
                errorInfoTv.setVisibility(View.GONE);
            }

            hideLoadDialog2();
        }
    }  ;

    private void updateChapters(List<BookPureText> books){
        bookShelfRv.setAdapter(new BookShelfAdapter(this, books, new SelectCallBack() {
            @Override
            public void onSelected(Object obj, int itemIndex) {
                if(obj instanceof BookPureText){
                    BookPureText book = (BookPureText) obj;
                    toGetPaper(book.getUrl());
                }
            }
        }));
    }

    private void toGetPaper(String bookUrl  ){
        ARouter.getInstance().build(RouteUtils.PureTextReaderActivity)
                .withString("bookUrl", bookUrl)
                .navigation();
    }

}
